package com.example.musicplayer.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.dao.IMusicHandler;
import com.example.musicplayer.dao.MusicHandler;
import com.example.musicplayer.dto.Song;


public class SongListActivity extends ListActivity {
	

	IMusicHandler getMusic;
	List<Song> allMusicList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		allMusicList = new ArrayList<Song>();
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SongListerTask listthread = new SongListerTask();
		listthread.execute();
		
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		Song selectedSong = (Song) getListAdapter().getItem(position);
		
		
	}		

	class CustomArrayAdapter extends ArrayAdapter<Song>{
	private Context context;
	private List<Song> objects;

	public CustomArrayAdapter(Context context, int resource, List<Song> objects) {
		super(SongListActivity.this, resource, objects);
		this.context = context;
		this.objects = objects;
		// TODO Auto-generated constructor stub
}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//each item in List view, if null creates a new view
		View individualItemView = convertView;
		if(individualItemView == null) {
			//assigns view a reference to layout
			individualItemView = layoutInflater.inflate(R.layout.new_listview_layout, parent, false);
			//creates viewholder object
			ViewHolder viewHolder = new ViewHolder();
			//assigns each view inside to viewholder variable- boiler plate logic for ViewHolder
			viewHolder.song = (TextView)individualItemView.findViewById(R.id.song);
			viewHolder.artist = (TextView)individualItemView.findViewById(R.id.artist);
			viewHolder.thumbnail = (ImageView)individualItemView.findViewById(R.id.thumbnail);
			individualItemView.setTag(viewHolder);
		}
		ViewHolder holder = (ViewHolder) individualItemView.getTag();
		holder.thumbnail.setImageURI(Uri.parse(getMusic.RetrieveAlbumArt(objects.get(position).getAlbumID())));
		holder.song.setText(objects.get(position).getSongName());
		holder.artist.setText(objects.get(position).getSongArtist());
		
		
		return individualItemView;
	}

}	//view holder class to make listview more efficient
	static class ViewHolder {

		 TextView song;
		 TextView artist;
		 ImageView thumbnail;
	}
	
	//class that handles adding music to listview using threading
	class SongListerTask extends AsyncTask<Void, Integer, ArrayAdapter<Song>> {

		@Override
		protected void onPostExecute(ArrayAdapter<Song> musicArrayAdapter) {
		
		setListAdapter(musicArrayAdapter);
		}
		@Override
		protected ArrayAdapter<Song> doInBackground(Void...voids) {
			getMusic = new MusicHandler(getContentResolver(),false, getBaseContext());
			allMusicList = getMusic.GetMusicList(false);
			ArrayAdapter<Song> musicArrayAdapter = new CustomArrayAdapter(SongListActivity.this, R.layout.new_listview_layout, allMusicList);
			
			
			
			
			
			
			return musicArrayAdapter;
		}
		
	}
}

